<?php

class searchController extends Controller {
   
    private $_page_number ;
    private $_sql;
    private $_sql2;
    private $_content;
    public function __construct($model, $action) {
        parent::__construct($model, $action);
       $this->_page_number = 4;
    }
    public function getSQL(){
        $this->_sql ="select * from product";
        $query1 = $query2 =$query3 = $query4 = $query5 =$query6 =null;
        if($_POST['idCategory']) 
            if($_POST['idCategory'] != 0)               
                 $query1 = "idCategory=".$_POST['idCategory'];
       
        if($_POST['productName'])
            $query2 = "  name like '%".$_POST['productName']."%'";
        
        if($_POST['dateStart'])
            $query3 = "  timeStart >= '".$_POST['dateStart']."' ";
        
        if($_POST['dateStartEnd'])
            $query4 = "  timeStart <= '".$_POST['dateStart']."' ";
        
        if($_POST['dateEnd'])
            $query5 = " timeEnd >= '".$_POST['dateEnd']."'";
        
        if($_POST['dateEndEnd'])
            $query6 = "  timeEnd >= '".$_POST['dateEndEnd']."'";
        
        if($query1 || $query2 || $query3 || $query4 || $query5 || $query6 )
        {
            $this->_sql .= ' where ';
            if($query1  )  {
                $this->_sql .= $query1;
                if($query2) {
                     $this->_sql .= ' and '. $query2;
                } 
                if($query3) {
                     $this->_sql .= ' and '. $query3;
                } 
                if($query4) {
                     $this->_sql .= ' and '. $query4;
                } 
                if($query5) {
                     $this->_sql .= ' and '. $query5;
                } 
                if($query6) {
                     $this->_sql .= ' and '. $query6;
                }   
            } else {
                  if($query2) {
                        $this->_sql .=  $query2;
                     if($query3) {
                           $this->_sql .= ' and '. $query3;
                     } 
                     if($query4) {
                           $this->_sql .= ' and '. $query4;
                     } 
                     if($query5) {
                           $this->_sql .= ' and '. $query5;
                     } 
                     if($query6) {
                           $this->_sql .= ' and '. $query6;
                     }   
                } else {
                        if($query3) {
                            $this->_sql .=  $query3;
                            if($query4) {
                               $this->_sql .= ' and '. $query4;
                            } 
                            if($query5) {
                                  $this->_sql .= ' and '. $query5;
                            } 
                            if($query6) {
                                  $this->_sql .= ' and '. $query6;
                            }    
                        } else {
                            if($query4) {
                                $this->_sql .=  $query4;
                                if($query5) {
                                  $this->_sql .= ' and '. $query5;
                                } 
                                if($query6) {
                                      $this->_sql .= ' and '. $query6;
                                }    
                            } else {
                                if($query5) {
                                    $this->_sql .=  $query5;
                                    if($query6) {
                                          $this->_sql .= ' and '. $query6;
                                    }   
                                } else {
                                    $this->_sql .=  $query6;
                                }
                            }
                        }
                    }
           }
        }
        $this->_sql = $this->getSQLSort($this->_sql);
        
          
    }
     
    public function getSQLSort($sql)
    {
        if(isset($_POST['sortby']) && isset($_POST['rank'])){       
                    $sortby = $_POST['sortby'];
                    $rank = $_POST['rank'];
                    $page = $_POST['page'];
                    if($sortby == '-1' || $sortby == '2') {
                                if($rank == '-1' || $rank == '2') {
                                       $sql .= ' order by timeStart DESC Limit '.($page* $this->_page_number).','. $this->_page_number;
                                } else {
                                       $sql .= ' order by timeStart ASC Limit '.($page* $this->_page_number).','. $this->_page_number;
                                }

                            }
                            
                    if($sortby == '1'){
                        if($rank == '-1' || $rank == '2') {
                               $sql .= ' order by name DESC Limit '.($page* $this->_page_number).','. $this->_page_number;
                        } else {
                               $sql .= ' order by name ASC Limit '.($page* $this->_page_number).','. $this->_page_number;
                        }
                    }

                    if($sortby == '3'){
                        if($rank == '-1' || $rank == '2')  {
                               $sql .= ' order by timeEnd DESC Limit '.($page* $this->_page_number).','. $this->_page_number;
                        } else {
                               $sql .= ' order by timeEnd ASC Limit '.($page* $this->_page_number).','. $this->_page_number;
                        }
                    }

                }  else {
                     $sql .= ' Limit '.($page* $this->_page_number).','. $this->_page_number;
                }
                
                 $this->_sql2 = $this->_sql;
                 return $sql;
    }
            
    public function getPagination()
    {
        $page = $_POST['page'];
        $count= $this->_productModel->getCount( $this->_sql2);
        $pageCounts=explode(".",$count/ $this->_page_number);
        $page_count = $pageCounts[0];

         $this->_content .='<div class="phantrang">';
         if($page!=0)
         {
              $this->_content .='<a href="javascript:sort(0,1)">Đầu</a>  |';

         }
        for($i =0; $i< $page_count ; $i++)
        {
             $this->_content .='<a href="javascript:sort('.$i.')">  |  '.($i+1).'</a>';
        }

         if($page <= $page_count-1 && isset($pageCounts[1]))
         {
              $this->_content .='|  <a href="javascript:sort('.  $page_count.')">Cuối<a/>';

         }
    }
    
    public function index()
    {
        $this->_content='';
        $this->_content .= "<div class='title_new_product'>";
        $this->_content .= "SẢN PHẨM ĐƯỢC TÌM THẤY";
        $this->_content .= "</div>";
        $this->_content .= "<div class='list_new_product'>";
        
        if(isset($_POST))  {
            $this->getSQL();
            $this->_memcached->delete('search'.md5( $this->_sql));
            if( $this->_memcached->get('search'.md5( $this->_sql)))
            {
               $prod_result=$this->_memcached->get('search'.  md5( $this->_sql));
               foreach ($prod_result as $item) {
               $this->_content .=' <a href="#" class="link_product">
                                        <div class="body_hp">
                                            <img class="img_product" src="/public/images/ex_product.gif" alt="'.$item['name'].'" />
                                            <a href ="/product/detail/'.$item["idProduct"].'"> <p class="title_product">'.$item['name'].'</p> </a>
                                            <p class="date_end_product">Ngày kết thúc: '.$item['timeEnd'].'</p>
                                            <div class="clear_float"></div>
                                        </div>
                                    </a>';
               }
            } else {
                
               $prod_result=  $this->_productModel->getSearch( $this->_sql);
               if(empty($prod_result))
               {
                   $this->_content .='<span style="color:red">Không tìm thấy vật phẩm</span>';
               } else {
               //var_dump($this->_sql);die();
                foreach ($prod_result as $item) {
                $this->_content .=' <a href="#" class="link_product">
                                         <div class="body_hp">
                                             <img class="img_product" src="/public/images/ex_product.gif" alt="'.$item['name'].'" />
                                             <a href ="/product/detail/'.$item["idProduct"].'"><p class="title_product">'.$item['name'].'</p></a>
                                             <p class="date_end_product">Ngày kết thúc: '.$item['timeEnd'].'</p>
                                             <div class="clear_float"></div>
                                         </div>
                                     </a>';
                }
                $this->_memcached->set('search'.  md5( $this->_sql),$prod_result);     
               
               }          
            }
 
            $this->getPagination();
        }   
       
        $this->_content .='</div>';
        $this->_setView("index");
        $this->_view->set("content", $this->_content);
        return $this->_view->output();
    }
    }
?>
